﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class testtt : System.Web.UI.Page
    {
        string groupID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //HANDLE THIS NULL...
            groupID = Request.QueryString["groupid"];

            if (!IsPostBack)
            {
                if (Request.Url != null)
                {
                    Session["PreviousPageUrl"] = Request.Url.ToString();
                }
                else
                {
                    Session["PreviousPageUrl"] = "~/index.aspx";
                }
            }

            //if (groupID == null)
            //{
            //    Response.Redirect("~/registration.aspx?redirecterror=1");
            //}

            if (Session["Member"] == null || groupID == null)
            {
                Session["flag"] = "redirecterror";
                Response.Redirect("~/registration.aspx");
            }

            if (Session["flag"] != null)
            {
                switch (Session["flag"].ToString())
                {
                    case "login":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome!');", true);
                        break;

                    case "firsttime":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome! You have been registered!');", true);
                        break;

                    case "newcreated":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your group has been created!');", true);
                        break;

                    case "joined":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have joined the group!');", true);
                        break;

                    case "leave":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have left the group!');", true);
                        break;

                    case "updated":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your group has been updated!');", true);
                        break;

                    case "pending":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You are now waiting to be accepted to the group!');", true);
                        break;

                    case "invited":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have invited friends to join the group!');", true);
                        break;
                }

                Session["flag"] = null;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "", true);
            }

            //if (Request.QueryString["login"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome!');", true);
            //}

            //if (Request.QueryString["firsttime"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome! You have been registered!');", true);
            //}

            //if (Request.QueryString["newcreated"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your group has been created!');", true);
            //}

            //if (Request.QueryString["joined"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have joined the group!');", true);
            //}

            //if (Request.QueryString["leave"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have left the group!');", true);
            //}

            //if (Request.QueryString["updated"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have updated the group!');", true);
            //}
            

            Group g = GroupControl.RetrieveGroup(groupID);

            if (g.GroupId == null)
            {
                Session["flag"] = "notfound";
                Response.Redirect("~/index.aspx");
            }


            lblViewGroupDetailsTitle.Text = g.GroupName;
            tbxViewGroupDesc.Text = g.Description;
            string privacy = "";
            switch (g.GroupPrivacy)
            {
                case 0:
                    privacy = "Public";
                    break;
                case 1:
                    privacy = "Private";
                    break;
                case 2:
                    privacy = "Secret";
                    break;
            }
            tbxViewGroupPrivacy.Text = privacy;
            tbxViewGroupNoOfPpl.Text = GroupControl.GetNumberofMembers(groupID).ToString();
            tbxViewGroupCategory.Text = DBMANAGER.GetGroupType(Convert.ToInt32(g.GroupTypeId));

            string id = DBMANAGER.GetImageIdGroupLogo(groupID);
            imgViewGroupLogo.ImageUrl = "~/HTTPHandler.ashx?imageid=" + Convert.ToInt32(id);

            #region ShowBtnJoin
            Session["GroupJoined"] = GroupControl.isJoin(groupID, Session["Member"].ToString());
            
            
            if (((List<MemberGroup>)Session["GroupJoined"]).Count != 0)
            {
                
                btnInviteGroup.Visible = true;
                btnLeaveGroup.Visible = true;
                

                if (privacy == "Secret")
                {
                    btnInviteGroup.Visible = false;
                }
                    if (GroupControl.IsAdmin((List<MemberGroup>)Session["GroupJoined"]))
                    {
                        btnEditGroup.Visible = true;
                        btnLeaveGroup.Visible = false;
                        btnInviteGroup.Visible = true;
                    }
                    else
                    {
                        btnEditGroup.Visible = false;
                    }
            }
            else
            {
                btnJoinGroup.Visible = true;
            }
            int Status = GroupControl.isPending((List<MemberGroup>)Session["GroupJoined"]);
            if (Status == 3) // if it is still pending
            {
                btnJoinGroup.Visible = true;
                btnJoinGroup.Enabled = false;
                btnJoinGroup.Text = "Waiting";
                btnEditGroup.Visible = false;
                btnInviteGroup.Visible = false;
                btnLeaveGroup.Visible = false;
            }
            else if (Status == 0)//if it is still waiting 
            {
                btnJoinGroup.Visible = true;
                btnJoinGroup.Enabled = true;
                btnJoinGroup.Text = "JOIN";
                btnEditGroup.Visible = false;
                btnInviteGroup.Visible = false;
                btnLeaveGroup.Visible = false;
            }
            else if (Status == 2)
            {
                btnJoinGroup.Visible = false;
                btnEditGroup.Visible = false;
                btnInviteGroup.Visible = false;
                btnLeaveGroup.Visible = false;
            }
            #endregion

            string twitterurl = Utility.GetServerUrl(Request) + "/ViewGroup.aspx?groupid={0}";

            //twitter.Attributes.Add("data-url", String.Format(twitterurl,eventid));

            twitter.Attributes["data-url"] = String.Format(twitterurl, groupID);

            //twitter.Attributes["data-url"] = "http://localhost:26473/ViewEvent.aspx?eventid=10";
        }

        protected void btnJoinGroup_Click(object sender, EventArgs e)
        {
            int status = 0;

            if (btnJoinGroup.Text != "JOIN")   // if 1 = public   // if 99 private
            {
                status = GroupControl.JoinGroup(groupID, Session["Member"].ToString());
            }
            else    //1
            {
                bool b = GroupControl.UpdateMemberGroupStatus(Session["Member"].ToString(), groupID, 1);

                if (b)
                {
                    status = 1;
                }
            }


            if (status == 1)
            {
                Session["flag"] = "joined";
                Response.Redirect("~/ViewGroup.aspx?groupid=" + groupID);
            }
            else
            {
                //Notifications

                int s = Convert.ToInt32(Session["Member"]);
                int r = GroupControl.FindAdmin(groupID);

                Utility.SendJoinInterestToGroupAdmin(s, r, groupID);

                Session["flag"] = "pending";
                Response.Redirect("~/ViewGroup.aspx?groupid=" + groupID);
            }
        }

        protected void btnLeaveGroup_Click(object sender, EventArgs e)
        {
            GroupControl.LeaveGroup(groupID, Session["Member"].ToString());

            Session["flag"] = "leave";
            Response.Redirect("~/ViewGroup.aspx?groupid=" + groupID);
        }

        protected void btnEditGroup_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UpdateGroup.aspx?groupid=" + groupID);
        }

        protected void btnInviteGroup_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InvitePplToGroup.aspx?groupid=" + groupID);
        }
    }
}